<?php

namespace frontend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\SetMealOrder;

/**
 * SetMealOrderSearch represents the model behind the search form of `common\models\SetMealOrder`.
 */
class SetMealOrderSearch extends SetMealOrder
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'member_id', 'set_meal_id', 'item_id', 'num', 'member_discount_coupon_id', 'status', 'is_use', 'pay_type', 'pay_date', 'pay_money', 'total_money', 'discount_money', 'created_at', 'updated_at'], 'integer'],
            [['sn', 'sub_date', 'time_frame', 'name', 'phone', 'address', 'city'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SetMealOrder::find()->with(["setMeal" => function($q) {
            $q->with(["setMealImages"]);
        }, "setMealItem"])->asArray()->orderBy("id desc");

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'member_id' => $this->member_id,
            'set_meal_id' => $this->set_meal_id,
            'item_id' => $this->item_id,
            'num' => $this->num,
            'member_discount_coupon_id' => $this->member_discount_coupon_id,
            'status' => $this->status,
            'is_use' => $this->is_use,
            'pay_type' => $this->pay_type,
            'pay_date' => $this->pay_date,
            'pay_money' => $this->pay_money,
            'total_money' => $this->total_money,
            'discount_money' => $this->discount_money,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'sn', $this->sn])
            ->andFilterWhere(['like', 'sub_date', $this->sub_date])
            ->andFilterWhere(['like', 'time_frame', $this->time_frame])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'city', $this->city]);

        return $dataProvider;
    }
}
